﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.Model.Enum;

#endregion

namespace WinnerCMS.WebUI.Admin.Photo
{
    [Access("ContenAdd,ContenEdit")]
    public partial class Add : WebFactory.PageBase
    {
        private WinnerCMS.Model.Photo model = new WinnerCMS.Model.Photo();

        private byte iModelId;

        /// <summary>
        ///     自定义字段
        /// </summary>
        protected bool ExtendField;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitEveryPost();

            if (!IsPostBack)
            {
                if (Request.QueryString["TempFlag"] == null && Request["ID"] == null)
                {
                    Response.Redirect(Request.RawUrl + "&TempFlag=" + Guid.NewGuid());
                }

                this.txtUpDateTime.Text = DateTime.Now.ToString();

                BindDropDownList();
                FieldEnabale();

                if (Request["ID"] != null) //修改界面
                {
                    EidtUI();
                }
                else //添加界面
                {
                    AddUI();
                }
            }
        }

        private void InitEveryPost()
        {
            this.iModelId = byte.Parse(Request["ModelId"]);

            if (Request["ID"] != null) //修改界面
            {
                Title = string.Concat("修改", ModelManage.GetContentName(this.iModelId));
            }
            else //添加界面
            {
                Title = string.Concat("添加", ModelManage.GetContentName(this.iModelId));
            }

            new FieldObject(this.iModelId).LoadUI(this.T_DefinedFile, out this.ExtendField);
        }

        #region  绑定DropDownList

        /// <summary>
        ///     绑定DropDownList
        /// </summary>
        private void BindDropDownList()
        {
            new CategoryManage().BindDDL(this.ddlCategory, BindOption.NoLink, "<请选择栏目>", this.iModelId);
            new SpecialManage().BindDDL(this.ddlSpecialID, BindOption.NoLink, "<请选择专题>", this.iModelId);
            new CategoryManage().BindDDL(this.lblCategoryMap, BindOption.NoLink, "<不选择>", this.iModelId);

            ServerControl.BindDropDownList(this.ddlRank, new RoleManage().BindList(false), "RoleName", "ID",
                new ListItem {Text = "<请选择>", Value = "0"});
        }

        #endregion

        private void FieldEnabale()
        {
            this.trAuthor.Visible = FieldControl.Enabale("Author");
            this.trFrom.Visible = FieldControl.Enabale("From");
            this.trHits.Visible = FieldControl.Enabale("Hits");
            this.trKey.Visible = FieldControl.Enabale("Key");

            this.trPicUrl.Visible = FieldControl.Enabale("PicUrl");
            this.trPointNum.Visible = FieldControl.Enabale("PointNum");
            this.trRank.Visible = FieldControl.Enabale("Rank");
            this.trSpecialID.Visible = FieldControl.Enabale("SpecialID");

            this.trTemplateFileName.Visible = FieldControl.Enabale("TemplateFileName");
            this.trUpDateTime.Visible = FieldControl.Enabale("UpDateTime");

            //////////////////////////////////////////


            if (FieldControl.Enabale("IncludePic") == false)
            {
                this.cbkIncludePic.Visible = false;
                this.cbkIncludePic.Checked = true;
            }

            if (FieldControl.Enabale("AllowComment") == false)
            {
                this.cbkAllowComment.Visible = false;
                this.cbkAllowComment.Checked = true;
            }
            this.cbkIsSlide.Visible = FieldControl.Enabale("IsSlide");
        }

        #region 添加界面

        /// <summary>
        ///     添加界面
        /// </summary>
        private void AddUI()
        {
            if (Session["Key"] != null || Session["SelectCategoryId"] != null)
            {
                this.txtKey.Text = Session["Key"].ToString();
            }

            this.hfAnnexFlag.Value = Request.QueryString["TempFlag"];
            this.ddlCategory.SelectedValue = Request.QueryString["CategoryId"];
            this.btnAdd.Visible = true;
            this.btnAdd.Text = string.Concat("添加", ModelManage.GetContentName(this.iModelId));

            this.btnEdit.Visible = false;
        }

        #endregion

        #region 修改界面

        /// <summary>
        ///     修改界面
        /// </summary>
        private void EidtUI()
        {
            this.model = new PhotoManage().GetInfo(int.Parse(Request["ID"]));

            this.ddlCategory.SelectedValue = this.model.CategoryId.ToString();
            this.txtTitle.Text = Server.HtmlDecode(this.model.ContentTitle);
            this.txtAuthor.Text = this.model.Author;
            this.txtFrom.Text = this.model.ComeFrom;
            this.txtKey.Text = this.model.Tags;
            this.CC_Content.Text = this.model.Content;
            this.txtPicUrl.Text = this.model.PicUrl;
            this.ddlRank.SelectedValue = this.model.Rank.ToString();
            this.NtxtPointNum.Text = this.model.PointNum.ToString();
            this.NtxtHits.Text = this.model.Hits.ToString();

            this.cbkIncludePic.Checked = this.model.IncludePic;
            this.cbkIsSlide.Checked = this.model.IsSlide;
            this.cbkAllowComment.Checked = this.model.AllowComment;
            this.cbkIsHeadline.Checked = this.model.IsHeadline;
            this.cbkIsOnTop.Checked = this.model.IsOnTop;
            this.cbkIsElite.Checked = this.model.IsElite;
            this.cbkIsChecked.Checked = this.model.IsChecked;;
            //cbkIsDraft.Checked = model.IsDraft;

            this.txtBrief.Text = this.model.Brief;
            this.txtTemplateFileName.Text = this.model.TemplateFileName;
            this.txtSEOTitle.Text = this.model.SEOTitle;
            this.txtSEOKeyWord.Text = this.model.SEOKeyWord;
            this.txtSEODescript.Text = this.model.SEODescript;

            this.btnAdd.Visible = false;
            this.btnEdit.Visible = true;
            this.btnEdit.Text = string.Concat("修改", ModelManage.GetContentName(this.iModelId));

            #region 自定义字段

            string strErr = null;
            new FieldManage().ExtendFieldData(this.iModelId, false, Request["ID"], this.T_DefinedFile, ref strErr);
            if (string.IsNullOrEmpty(strErr) == false)
                MsgBox.Message(strErr.Replace("'", "\\'"), this);

            #endregion
        }

        #endregion

        #region 添加修改

        /// <summary>
        ///     添加修改
        /// </summary>
        private void Save()
        {
            var Fields = new List<string>();
            var Values = new List<object>();
            foreach (TableRow tr in this.T_DefinedFile.Rows)
            {
                var c = tr.Cells[1].Controls[0];
                if (c is TextBox)
                {
                    var tb = c as TextBox;
                    Fields.Add(tb.ID);
                    Values.Add(tb.Text.Trim());
                }
                if (c is DropDownList)
                {
                    var ddl = c as DropDownList;
                    Fields.Add(ddl.ID);
                    Values.Add(ddl.SelectedValue);
                }
                if (c is RadioButtonList)
                {
                    var rbl = c as RadioButtonList;
                    Fields.Add(rbl.ID);
                    Values.Add(rbl.SelectedValue);
                }
                if (c is CheckBox)
                {
                    var cb = c as CheckBox;
                    Fields.Add(cb.ID);
                    Values.Add(cb.Checked);
                }
            }

            this.model.ContentTitle = this.txtTitle.Text.Trim();
            this.model.Author = this.txtAuthor.Text.Trim();
            this.model.ComeFrom = this.txtFrom.Text.Trim();
            this.model.Editor = UserManage.Current.UserName;
            this.model.Tags = this.txtKey.Text.Trim();


            this.model.Content = this.CC_Content.Text;
            this.model.PicUrl = this.txtPicUrl.Text.Trim();
            this.model.CategoryId = int.Parse(this.ddlCategory.SelectedValue);
            var categores = new List<string>();
            foreach (ListItem li in this.lblCategoryMap.Items)
            {
                if (li.Selected)
                {
                    categores.Add(li.Value);
                }
            }
            this.model.CategoryIds = string.Join(",", categores.ToArray());

            this.model.IncludePic = this.cbkIncludePic.Checked;
            this.model.IsSlide = this.cbkIsSlide.Checked;
            this.model.AllowComment = this.cbkAllowComment.Checked;
            this.model.IsHeadline = this.cbkIsHeadline.Checked;
            this.model.IsOnTop = this.cbkIsOnTop.Checked;
            this.model.IsElite = this.cbkIsElite.Checked;
            //model.IsChecked = cbkIsChecked.Checked;
            //model.IsDraft = cbkIsDraft.Checked ;

            this.model.Rank = short.Parse(this.ddlRank.SelectedValue);
            this.model.PointNum = short.Parse(this.NtxtPointNum.Text.Trim());
            this.model.Hits = int.Parse(this.NtxtHits.Text.Trim());
            this.model.UpdateTime = DateTime.Parse(this.txtUpDateTime.Text);
            this.model.Brief = this.txtBrief.Text.Trim();
            this.model.TemplateFileName = this.txtTemplateFileName.Text.Trim();

            this.model.SEOTitle = this.txtSEOTitle.Text;
            this.model.SEOKeyWord = this.txtSEOKeyWord.Text;
            this.model.SEODescript = this.txtSEODescript.Text;
            this.model.Id = Convert.ToInt32(Request["ID"]);


            var resu = new PhotoManage().Add(this.model, this.hfAnnexFlag.Value, Fields, Values);
            if (string.IsNullOrEmpty(resu.Errors) == false)
            {
                MsgBox.Message(resu.Errors, this);
            }
            else
            {
                Response.Redirect(string.Format("../Common/SaveOK.aspx?ModelId={0}&ID={1}", this.iModelId, this.model.Id));
            }
        }

        #endregion

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            Save();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Save();
        }
    }
}